package com.xrkj.b2c.controller.sys;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.xrkj.b2c.controller.common.BasicController;
import com.xrkj.b2c.model.bus.BusOrder;
import com.xrkj.b2c.model.sys.SysCustomer;
import com.xrkj.b2c.service.bus.BusOrderService;
import com.xrkj.b2c.service.sys.SysCustomerService;
import com.xrkj.b2c.utils.StringUtil;
import com.xrkj.b2c.utils.UUIDUtil;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>客户管理<BR>
 * <B>概要说明：</B><BR>
 * 
 * @author knn
 * @since 2018年7月10日
 */
@RequestMapping("customer")
@Controller
public class SysCustomerController extends BasicController {

    @Autowired
    private SysCustomerService sysCustomerService;

    private Logger logger = Logger.getLogger(SysCustomerController.class);

    /**
     * 
     * <B>方法名称：</B>客户列表<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月10日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardCustomer")
    public ModelAndView forwardOrderManager(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/bus/customer/customer-to-manager");
    }


    /**
     * 
     * <B>方法名称：</B>获取客户列表<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月10日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/customerList")
    @ResponseBody
    public Object customerList(HttpServletRequest request, HttpServletResponse response) {
        String search = request.getParameter("search");// 名称
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("offset", StringUtil.isEmpty(offset) ? null : Integer.parseInt(offset));
        paramter.put("limit", StringUtil.isEmpty(limit) ? null : Integer.parseInt(limit));
        paramter.put("search", search);
        Integer total = sysCustomerService.findCustomerTotal(paramter);
        List<SysCustomer> customers = new ArrayList<SysCustomer>();
        if (total > 0) {
            customers = sysCustomerService.findCustomerList(paramter);
        }
        ret.put("total", total);
        ret.put("rows", customers);
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>添加客户窗口<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月10日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardAddCustomer")
    public ModelAndView forwardAddCustomer(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/bus/customer/add-customer-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>添加客户<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月10日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/addCustomer")
    @ResponseBody
    public Map<String, Object> addCustomer(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        SysCustomer customer = new SysCustomer();
        customer.setcId(UUIDUtil.id());
        customer.setcNo(request.getParameter("cNo"));
        customer.setcName(request.getParameter("cName"));
        customer.setcContact(request.getParameter("cContact"));
        customer.setcMobile(request.getParameter("cMobile"));
        customer.setcCreateTime(new Date());
        customer.setcAddress(request.getParameter("cAddress"));
        if (sysCustomerService.insert(customer) > 0) {
            ret.put("success", true);
            ret.put("msg", "保存成功！");
        } else {
            ret.put("success", false);
            ret.put("msg", "保存失败！");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>修改客户窗口<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月11日
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardUpdateCustomerPage")
    public ModelAndView forwardUpdateCustomerPage(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("loadCustomer", sysCustomerService.selectCustomerById(request.getParameter("id")));
        return new ModelAndView("WEB-INF/pages/bus/customer/update-customer-dialog");
    }

    /**
     * 
     * <B>方法名称：</B>修改客户<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月12日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/updateCustomer")
    @ResponseBody
    public Map<String, Object> updateCustomer(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        SysCustomer customer = new SysCustomer();
        customer.setcId(request.getParameter("cId"));
        customer.setcNo(request.getParameter("cNo"));
        customer.setcName(request.getParameter("cName"));
        customer.setcContact(request.getParameter("cContact"));
        customer.setcMobile(request.getParameter("cMobile"));
        customer.setcAddress(request.getParameter("cAddress"));
        customer.setcUpdateTime(new Date());
        if (sysCustomerService.update(customer) > 0) {
            ret.put("success", true);
            ret.put("msg", "修改成功！");
        } else {
            ret.put("success", false);
            ret.put("msg", "修改失败！");
        }
        return ret;
    }

    /**
     * 
     * <B>方法名称：</B>删除客户<BR>
     * <B>概要说明：</B><BR>
     * 
     * @author knn
     * @since 2018年7月12日
     * @param request
     * @param response
     * @param pageSize
     * @param pageNo
     * @return
     */
    @RequestMapping("/delCustomer")
    @ResponseBody
    public Map<String, Object> delCustomer(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
        String id = request.getParameter("cId");
        if (sysCustomerService.deleteByPrimaryKey(id) > 0) {
            // 删除成功
            ret.put("success", true);
            ret.put("msg", "删除成功。");
        } else {
            // 删除失败
            ret.put("success", false);
            ret.put("msg", "删除失败");
        }
        return ret;
    }

    @Autowired
    private BusOrderService busOrderService;


    /**
     * select2 下拉框数据
     *
     * @param id 订单id
     * @return json
     */
    @RequestMapping("/select2Customer")
    @ResponseBody
    public Map<String, Object> select2Customer(String id) {
        List<SysCustomer> customerList = sysCustomerService.findCustomerList(null);
        BusOrder busOrder = busOrderService.selectByPrimaryKey(id);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysCustomer sysCustomer : customerList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", sysCustomer.getcId());
            map.put("text", sysCustomer.getcName());
            if (!StringUtil.isBlank(id) && busOrder != null && !StringUtil.isBlank(busOrder.getoCustomerId()) && busOrder.getoCustomerId().equals(sysCustomer.getcId())) {
                map.put("selected", true);
            }
            list.add(map);
        }
        Map<String, Object> results = new HashMap<>(1);
        results.put("results", list);
        return results;
    }
}
